﻿using System;
using System.Text;
using System.Text.RegularExpressions;
using AllInOnePluginBL.Helpers;
using Rss;

namespace AllInOnePluginBL.SiteScrappers
{
    /// <summary>
    /// Summary description for SratimoNet
    /// </summary>
    public class SratimoNet
    {
        private const string DEFAULT_URL = "http://www.sratimo.net/";
        protected const string SEARCH_URL = "http://www.sratimo.net/index.php?s={0}";

        public SratimoNet()
        {
         //   this.Name = "Sratimo.Net";
        }

        #region Private Methods
        private RssFeed LoadCategories(string url)
        {
            try
            {
                var page = Utils.GetPage(url, Encoding.UTF8);

                var startOfCategories = page.IndexOf("<div class=\"bar\">");
                var endOfCategories = page.IndexOf("</div>", startOfCategories);
                var categoriesSection = page.Substring(startOfCategories, endOfCategories - startOfCategories);

                const string moviesContainerExpression = "<a href=\"(.+?)\".*?>(.+?)</a>";
                var exp = new Regex(moviesContainerExpression, RegexOptions.Singleline);
                var matches = exp.Matches(categoriesSection);
                foreach (Match match in matches)
                {
                    var moviePageLink = match.Groups[1].Value;
                    var movieTitle = match.Groups[2].Value;

                    //movie.MovieLinks.Add(moviePageLink);

                    //var movieItems = movie.GetRssItems(Utils.GetSiteUrl(), this._pluginId, (int)PluginMode.MOVIES_MODE);
                    //foreach (var movieItem in movieItems)
                    //{
                    //    rssChannel.Items.Add(movieItem);
                    //}
                }
                
                return null;
            }
            catch (Exception ex)
            {
                return null;
            }
        }

        private RssFeed LoadMovies(string url)
        {
            try
            {
                var page = Utils.GetPage(url, Encoding.UTF8);

                const string moviesContainerExpression = "<div class=\"myarticle_cat\">.*?<a href=\"(.+?)\".*?title=\"(.+?)\"><img src=\"(.+?)\".*?</h2>(.+?)</font>";
                var exp = new Regex(moviesContainerExpression, RegexOptions.Singleline);
                var matches = exp.Matches(page);
                foreach (Match match in matches)
                {
                    var moviePageLink = match.Groups[1].Value;
                    var movieTitle = match.Groups[2].Value;
                    var imageLink = match.Groups[3].Value;
                    var plot = match.Groups[4].Value;

                    //var movie = new Movie
                    //                {
                    //                    Name = movieTitle,
                    //                    ImageLink = imageLink,
                    //                    Plot = plot
                    //                };

                    //var pageContent = Utils.GetPage(moviePageLink, Encoding.UTF8);
                    //movie.MovieLinks = GetMovieLinks(pageContent);

                    //var movieItems = movie.GetRssItems(Utils.GetSiteUrl(), this._pluginId, (int)PluginMode.MOVIE_MODE);
                    //foreach (var movieItem in movieItems)
                    //{
                    //    //var rssCategory = new RssCategory { Name = "MegaVideo" };
                    //    //movieItem.Categories.Add(rssCategory);
                    //    rssChannel.Items.Add(movieItem);
                    //}
                }

                return null;
            }
            catch (Exception ex)
            {
                return null;
            }
        }

        private string GetNextPageLink(string pageContent)
        {
            try
            {
                const string nextPageExpression = "class=\"numeric_onclick\">.*?<a href=\"(.+?)\"";
                var exp = new Regex(nextPageExpression, RegexOptions.Singleline);
                var match = exp.Match(pageContent);
                if (match.Success)
                {
                    return match.Groups[1].Value.Trim();
                }
            }
            catch { }

            return string.Empty;
        }
        #endregion
    }
}